import uuid
from dataclasses import dataclass
from typing import Any

import pytest
from sqlalchemy import insert
from sqlalchemy.ext.asyncio import AsyncSession

from recruit.adapter.database.gateway import ResumeSalaryGateway
from recruit.adapter.database.models import Resume, ResumeSalary
from .fixtures.resume import ResumeTest
from .utils import check_attr_model, delete_user, check_get_one, get_resume


@dataclass
class ResumeSalaryTest:
    id: uuid.uuid4()
    resume_id: uuid.uuid4()
    salary: str


async def _check_resume(
        session: AsyncSession,
        data: ResumeSalaryTest
) -> None:
    resume_salary = await get_resume(session, ResumeSalary, data.id)
    assert isinstance(resume_salary, ResumeSalary)
    check_get_one(
        resume_salary,
        data,
        ('id', 'resume_id', 'salary')
    )


async def _create_resume_salary(
        session: AsyncSession,
        data: dict[str, Any]
) -> ResumeSalaryTest:
    stmt = insert(ResumeSalary).values(**data).returning(ResumeSalary)
    resume_salary = (await session.execute(stmt)).scalar_one()
    resume_salary = ResumeSalaryTest(
        id=resume_salary.id,
        resume_id=resume_salary.resume_id,
        salary=resume_salary.salary
    )
    await session.commit()
    return resume_salary


async def test_create(
        db_session: AsyncSession,
        get_resume_salary: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_salary_gateway = ResumeSalaryGateway(db_session)
    get_resume_salary['resume_id'] = create_resume.id
    resume_salary = await resume_salary_gateway.create(**get_resume_salary)
    assert isinstance(resume_salary.resume, Resume)
    check_attr_model(resume_salary, get_resume_salary)
    await db_session.commit()
    await delete_user(db_session, create_resume.user_id)


@pytest.mark.parametrize(
    'keys', [
        ('id', 'resume_id', 'salary')
    ]
)
async def test_get_one(
        keys: tuple[str, ...],
        db_session: AsyncSession,
        get_resume_salary: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_salary_gateway = ResumeSalaryGateway(db_session)
    get_resume_salary['resume_id'] = create_resume.id
    created_resume_salary = await _create_resume_salary(
        db_session,
        get_resume_salary
    )
    for key, value in get_resume_salary.items():
        resume_salary = await resume_salary_gateway.get_one(**{key: value})
        assert resume_salary is not None
        check_get_one(resume_salary, created_resume_salary, keys)
    await delete_user(db_session, create_resume.user_id)


async def test_check_resume(
        db_session: AsyncSession,
        get_resume_salary: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_salary_gateway = ResumeSalaryGateway(db_session)
    get_resume_salary['resume_id'] = create_resume.id
    created_resume_salary = await _create_resume_salary(
        db_session,
        get_resume_salary
    )
    val = await resume_salary_gateway.check_resume(
        created_resume_salary.resume_id
    )
    assert val
    await delete_user(db_session, create_resume.user_id)
    val = await resume_salary_gateway.check_resume(
        created_resume_salary.resume_id
    )
    assert not val


async def test_delete(
        db_session: AsyncSession,
        get_resume_salary: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_salary_gateway = ResumeSalaryGateway(db_session)
    get_resume_salary['resume_id'] = create_resume.id
    created_resume_salary = await _create_resume_salary(
        db_session,
        get_resume_salary
    )
    await resume_salary_gateway.delete(created_resume_salary.id)
    await db_session.commit()
    await delete_user(db_session, create_resume.user_id)
    resume_salary = await get_resume(
        db_session,
        ResumeSalary,
        created_resume_salary.id
    )
    assert resume_salary is None
